package com.redwine.app.utils;

import android.app.Activity;
import android.util.DisplayMetrics;
import android.util.TypedValue;

import com.redwine.app.APP;


/**
 * Created by gassion on 17/3/9.
 */
public class DimenUtils {


    /**
     * 获取屏幕的宽度
     */
    public final static int getWindowsWidth(Activity activity) {
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }

    /**
     * dp 转 px
     *
     * @param dp
     * @return
     */
    public static int dp2px(int dp) {
        int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, APP.getInstance().getResources().getDisplayMetrics());

        return px;
    }

    /**
     * dp 转 sp
     *
     * @param dp
     * @return
     */
    public static int dp2sp(int dp) {
        int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, dp, APP.getInstance().getResources().getDisplayMetrics());

        return px;
    }

    public static int dip2px(float dipValue) {
        final float scale = APP.getInstance().getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    public static int px2dp(float pxValue) {
        final float scale = APP.getInstance().getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}
